import {createRouter, createWebHashHistory} from 'vue-router'
// 路由中使用store
// import store from "@/store"
import Cookies from 'js-cookie'
import {dashboardRouter} from "@/router/modules/dashboard"

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/dashboard/index.vue'),
      meta: {title: '登录'}
    },
    dashboardRouter
  ]
})
const whiteList = ['/login']
router.beforeEach(async (to, from, next) => {
  const hasToken = Cookies.get('token')
  if (hasToken) {
    if (to.path === '/login') {
      next({path: '/'})
    } else {
      // await store.dispatch('userModule/getInfo')
      next()
    }
  } else { // 没有Token的情况
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
  document.title = to.meta.title as string
})

export default router
